package br.com.sgi.util.email;



import java.io.Serializable;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* classe EmailService.
*
*/
public class EmailService implements Serializable {

    /** serialVersionUID. */
    private static final long serialVersionUID = 5178342888082656092L;
    

    /*
      servidor="imap.gmail.com"
      conta="error@gruposim.com.br"
      password="error122122"
    */
    
    /** email Smtp Host. */
  //  private String emailSmtpHost = "imap.gmail.com";
    private String emailSmtpHost = "mail.sigeiadm@sigei.com.br";

    /** email Smtp Timeout. */
    private Integer emailSmtpTimeout = 100000;

    /** email Remetente Endereco. */
    //private String emailRemetenteEndereco = "horacio.jose.barros@gmail.com";
    private String emailRemetenteEndereco = "sigeiadm@sigei.com.br";
    
    private String password="sgi@12";
    
    /** email Remetente Nome. */
    private String emailRemetenteNome = "WEBSKIN - Nova Senha";

    /** emailAssunto. */
    private String emailAssunto;
   
    /** porta. */
   // private int mailSMTPServerPort = 465;
    private int mailSMTPServerPort = 587;

    /** . */
    /**
    * Enviar e-mail simples de escopo padro, contendo o remetente, desti
    * natrio, assunto e mensagem.
    *
    * @param to
    * @param from
    * @param subject
    * @param message
    * @param toName
    * @throws EmailException
    */
    public void send(String to, String toName, String message)
       {
   
       final Properties props = System.getProperties();
       props.put("mail.smtp.starttls.enable","true");
       props.put("mail.smtp.host", emailSmtpHost);
       props.put("mail.smtp.port", mailSMTPServerPort);
       props.put("mail.smtp.auth", "true");
       props.put("mail.debug", "true");
       props.put("mail.smtp.starttls.enable","false" );
       props.put("mail.smtp.password",password);
       
       final Session session = Session.getInstance(props, new Authenticator() {
           @Override
           protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(emailRemetenteEndereco, password);
           }
       });
       
       
       
       final MimeMessage email = new MimeMessage(session);
       try {
           email.setFrom(new InternetAddress(emailRemetenteEndereco));
           email.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
           email.setSubject("webskin - Esqueceu a senha?");
           
           email.setText(message);
           Transport.send(email);
       } catch (final AddressException e) {
           e.printStackTrace();
       } catch (final MessagingException e) {
           e.printStackTrace();
       }

    }

}
